<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  confirm-text="打印"
  title="批量打印"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateModel"
  size="large"
)
  data-page(
      v-bind="pageParams"
      )
  mt-table(
      v-bind="tableProps"
      style="margin-top: 10px;"
    )
</template>

<script setup lang="ts">
import { type QueryPagination, useApi } from "@scmt/core-api";
import { useQuery } from "@scmt/common-hooks";
import {
  type FormItemProps,
  type IAction,
  type DataPageProps,
  type TableProps,
} from "@scmt/common-components";
import { type SpecimenQueryParams } from "@scmt/api-lis-specimen";
//添加 Props属性
const $api = useApi();

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const queryPramas = reactive<SpecimenQueryParams>({
  pageNum: 1,
  pageSize: 20,
});
const updateModel = () => {
  appmodel.value = false;
};

const { query, data, loadingMap } = useQuery({
  tableData: (q: any) => {
    const params = Object.assign({}, queryPramas, q);
    return $api.lis.specimen.query(params).then((res) => {
      emit("message-to-parent", res);
      return res;
    });
  },
});

const pageParams = computed<DataPageProps>(() => {
  const queryFields: FormItemProps[] = [
    {
      prop: "inspectionTime",
      label: "检验时间",
      as: "date",
    },
    {
      prop: "specimenNumber",
      label: "标本号",
    },
    {
      prop: "billingDepartment",
      label: "开单机构",
      as: "select",
      options: {
        loadMethod: () => $api.upms.org.listAllOptions(),
      },
    },
    {
      prop: "billingDepartment",
      label: "开单科室",
      as: "select",
      options: {
        loadMethod: () => $api.upms.org.listAsOptions(),
      },
    },
    {
      prop: "deviceId",
      label: "检验仪器",
      as: "select",
      options: {
        loadMethod: () => $api.lis.instrument.listAsOptions(),
      },
    },
    {
      prop: "print",
      label: "打印状态",
      as: "select",
      options: {
        loadMethod: () => {
          return [
            {
              label: "未打印",
              value: 0,
            },
            {
              label: "已打印",
              value: 1,
            },
          ];
        },
      },
    },
    {
      prop: "patientType",
      label: "患者类型",
      as: "select",
      options: {
        loadMethod: () => $api.dict.item.listAsOptions("patient-type"),
      },
    },
    {
      prop: "urgent",
      label: "",
      as: "checkbox-group",
      options: {
        loadMethod: () => {
          return [
            {
              label: "加急",
              value: 1,
            },
          ];
        },
      },
    },
  ];

  const dataPageProps: DataPageProps = {
    loading: loadingMap.tableData,
    queryModel: queryPramas,
    queryFields,
    onQuery: () => query("tableData"),
    onAct: onPageActions,
    columns: 4,
  };

  return dataPageProps;
});

const tableProps = computed(() => {
  const { tableData } = toRefs(data);

  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData.value?.items,
    pagination: tableData.value?.pagination,
    insertIndex: false,
    columns: [
      {
        type: "selection",
      },
      {
        prop: "specimenNumber",
        label: "标本号",
      },
      {
        prop: "applicationProject",
        label: "申请项目",
      },
      {
        prop: "patientName",
        label: "姓名",
      },
      {
        prop: "sex",
        label: "性别",
      },
      {
        prop: "inspectionTime",
        label: "检验日期",
        format: "datetime",
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) => query("tableData", pagination),
    onSelectionChange: (data) => {},
  };
  return tableProps;
});
const onPageActions = (action: IAction, model?: any) => {
  switch (action.event) {
    case "add":
      break;
    case "destroy":
      break;
  }
};

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      query("tableData");
    }
  },
  {
    immediate: true,
  }
);
</script>
